package com.yanqu.road.web.controller.game;


import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eSexType;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.web.controller.BaseGameHttpServlet;
import com.yanqu.road.web.mgr.TempNameMgr;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/getRandomName")
public class PlayerRandomName extends BaseGameHttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpGameWebParam httpGameWebParam = super.parseRequest(request);
        JSONObject paramObj = httpGameWebParam.getParamObj();
        String param = httpGameWebParam.getParam();
        String language = httpGameWebParam.getLanguage();

        int sex = 0;
        if (paramObj.getString("sex") != null) {
            sex = paramObj.getInteger("sex");
        }
        eSexType sexType = eSexType.parse(sex);
        if (sexType == null) {
            JSONObject resultObj = getResultObj(GameErrorCode.E_REQUEST_ARGUMENT_ERROR, language);
            sendMessage(response, resultObj.toJSONString());
            return;
        }
        String name = TempNameMgr.getRandomName(sexType, language);
        JSONObject resultObj = getResultObj(0, language);
        resultObj.put("name", name);
        sendMessage(response, resultObj.toJSONString());
    }
}
